package com.ruoyi.file.log

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.pattern.ClassicConverter
import ch.qos.logback.classic.spi.ILoggingEvent

const val END_COLOR = "\u001b[m"

const val ERROR_COLOR = "\u001b[0;31m"

const val WARN_COLOR = "\u001b[0;33m"

/**
 * 日志模板：基于日志级别的颜色的转换器
 */
class LevelColorClassicConverter : ClassicConverter() {


    override fun convert(event: ILoggingEvent): String =
        "${getColor(event.level)}${String.format("%5s", event.level)}$END_COLOR"

    /**
     * Returns the appropriate characters to change the color for the specified logging
     * level.
     */
    private fun getColor(level: Level): String {
        return when (level.toInt()) {
            Level.ERROR_INT -> ERROR_COLOR
            Level.WARN_INT -> WARN_COLOR
            else -> ""
        }
    }
}


